using System;
using System.Security.Principal;

namespace BeeldBank
{
	/// <summary>
	/// Class representing properties of the currently logged in user.
	/// </summary>
	public class Principal : IPrincipal
	{
		private IIdentity _identity;
		private string[] _roles;

		#region Constructors
		/// <summary>
		/// Creates a new instance of Principal
		/// </summary>
		/// <param name="identity"></param>
		/// <param name="roles"></param>
		public Principal(IIdentity identity, string[] roles)
		{
			_identity = identity;
			_roles = new string[roles.Length];
			roles.CopyTo(_roles, 0);
			Array.Sort(_roles);
		}

		/// <summary>
		/// Creates a new instance of Principal
		/// </summary>
		/// <param name="identity"></param>
		/// <param name="role"></param>
		public Principal(IIdentity identity, string role)
		{
			_identity = identity;
			_roles = new string[1];
			_roles[1] = role;
		}
		#endregion

		#region IsInRole(string role)
		/// <summary>
		/// Checks whether a user is in a specified role.
		/// </summary>
		/// <param name="role"></param>
		/// <returns></returns>
		public bool IsInRole(string role)
		{
			return Array.BinarySearch( _roles, role ) >=0 ? true : false;
		}
		#endregion

		#region GetRoles()
		/// <summary>
		/// Returns all user roles within one string separated by a |-sign
		/// </summary>
		/// <returns></returns>
		public string GetRoles()
		{
			string roles = "";
			foreach (string role in _roles)
			{
				roles += role + "|";
			}
			return roles;
		}
		#endregion 

		#region Identity
		/// <summary>
		/// Returns the identity of the user.
		/// </summary>
		public IIdentity Identity
		{
			get { return _identity; }
		}
		#endregion

		#region CultureInfo
		private System.Globalization.CultureInfo _cultureInfo;
		/// <summary>
		/// Gets or sets the current users CultureInfo.
		/// </summary>
		public System.Globalization.CultureInfo CultureInfo
		{
			get
			{
				if (_cultureInfo == null)
				{
					return new System.Globalization.CultureInfo("en-US");
				}
				else
				{
					return _cultureInfo;
				}
			}
			set
			{
				_cultureInfo = value;
			}
		}
		#endregion

		#region UserId
		private int _userid;
		/// <summary>
		/// Gets or sets the userid of the currently logged in user.
		/// </summary>
		public int UserId
		{
			get
			{
				return _userid;
			}
			set
			{
				_userid = value;
			}
		}
		#endregion

		#region UserName
		private string _userName;
		/// <summary>
		/// Gets or sets the UserName of the currently logged in user.
		/// </summary>
		public string UserName
		{
			get
			{
				return _userName;
			}
			set
			{
				_userName = value;
			}
		}
		#endregion

		#region FullName
		private string _fullName;
		/// <summary>
		/// Gets or sets the Full name of the currently logged in user.
		/// </summary>
		public string FullName
		{
			get
			{
				return _fullName;
			}
			set
			{
				_fullName = value;
			}
		}
		#endregion

	}
}
